<template>
	<div class="wx-back-container">
		<div class="wx-back-box">
			<div class="loading-spinner" v-if="loading">
				<div class="spinner"></div>
				<div class="tech-grid"></div>
			</div>
			<div class="success-icon" v-if="bindSuccess">
				<i class="el-icon-success"></i>
			</div>
			<h2 :class="{ 'success-message': bindSuccess }" class="message mb20">{{ msg }}</h2>
			<img src="@/assets/images/qrcode_for_gh.jpg" style="width: 150px;height:150px;">
			<p class="success-subtitle mt20" style="color: #e2e8f0;">长按识别二维码，关注我们获取更多权益~</p>
			<div class="tech-elements" v-if="!bindSuccess && !loading">
				<div class="tech-dot"></div>
				<div class="tech-dot"></div>
				<div class="tech-dot"></div>
			</div>
		</div>
		<div class="floating-elements">
			<div class="floating-element"></div>
			<div class="floating-element"></div>
			<div class="floating-element"></div>
		</div>
	</div>
</template>

<script>
import { getUserInfo } from '@/api/wechat';
import { getCustomerByOpenId, updateCustomer } from "@/api/demo/customer";  // 导入用户信息API

export default {
	data() {
		return {
			weChatCode: null,
			msg: "微信获取授权...",
			loading: true,
			bindSuccess: false,
			tempToken: null,

		}
	},

	created() {
		// console.log("this.$route.query.code==", this.$route.query.code);
		// return
		if (this.$route.query.code) {
			this.weChatCode = this.$route.query.code;
						this.tempToken = this.$route.query.tempToken;

		} else {
			// http://mr.w3zo.com/?code=071W6MGa1RNCxK0ehdGa1JiGej2W6MG1&state=STATE#/wechat/callback?deptId=100
			// 手动解析URL参数，因为微信可能将参数放在hash前面
			const urlParams = this.getUrlParams(window.location.href);
			console.log("urlParams", urlParams)
			if (urlParams.code) {
				this.weChatCode = urlParams.code;
			}
		}
		console.log("this.$route.query.code==", this.$route.query.code);
		this.judgeLocation();
	},
	methods: {
		// 添加手动解析URL参数的方法
		getUrlParams(url) {
			const params = {};
			const parser = document.createElement('a');
			parser.href = url;
			const query = parser.search.substring(1);
			const vars = query.split('&');
			for (let i = 0; i < vars.length; i++) {
				const pair = vars[i].split('=');
				params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
			}
			return params;
		},

		async judgeLocation() {
			if (this.weChatCode) {
				console.log("开始获取用户信息...", this.weChatCode);
				// return
				// debugger

				getUserInfo({ code: this.weChatCode, tempToken: this.tempToken , deptId: this.$route.query.deptId, userId: this.$route.query.userId }).then(res => {
					const data = res.data;
					console.log("微信授权返回信息res==", data);
					if (data) {
						// 成功获取用户信息，进行后续处理
						// debugger
						// this.$router.push('/app?open='+data.openId+'&deptId='+data.deptId);
						getCustomerByOpenId({ openId: data.openId, deptId: data.deptId, userId: data.userId }).then(res => {
							this.loading = false;
							this.bindSuccess = true;
							this.msg = "登录成功";
							this.$message({
								message: "登录成功，您可以关闭手机页面，回到电脑端继续操作",
								type: "success",
								duration: 3000,
								customClass: 'success-message-popup'
							});

						});
					} else {
						console.log('未获取到有效的用户信息');
						this.$message.error("未获取到有效的用户信息")
						this.loading = false;
						this.msg = "登录失败";
					}
				}).catch(error => {
					console.log('请求用户信息失败：', error);
					this.$message.error("请求用户信息失败，请稍后重试")
					this.loading = false;
					this.msg = "登录失败";
				});
			}
			else {
				console.log("未获取到授权CODE，请稍后重试")
				this.$message.error("未获取到授权CODE，请稍后重试")
				this.loading = false;
				this.msg = "登录失败";
			}
		},
	}
};
</script>
